package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.dto.QuestionBatchDto;
import com.jyy.prefabricated.dto.QuestionDto;
import com.jyy.prefabricated.entity.Question;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.QuestionService;
import com.jyy.prefabricated.utils.UserSessionUtil;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 题目
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Resource
    private UserUtils userUtils;
    /**
     * 新增题目
     * @param questionDto
     * @return
     */
    @PostMapping("/questions")
    public AjaxResult insert(@Valid @RequestBody QuestionDto questionDto){
        log.info("Rest request to insert question:{}",questionDto);
        int id = questionService.insert(questionDto);
        Question result = questionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 批量新增题目
     * @param dto
     * @return
     */
    @PostMapping("/questions-batch")
    public AjaxResult insert(@Valid @RequestBody QuestionBatchDto dto){
        log.info("Rest request to batch insert question:{}",dto);
        int row = questionService.batchInsert(dto.getQuestions());
        return AjaxResult.success("成功导入" + row + "条数据");
    }

    /**
     * 更新题目
     * @param question
     * @return
     */
    @PutMapping("/questions")
    public AjaxResult update(@Valid @RequestBody QuestionDto question){
        log.info("Rest request to update question:{}",question);
        questionService.update(question);
        Question result = questionService.selectById(question.getId());
        return AjaxResult.success(result);
    }
    /**
     * 复制转移题目
     * @param question 题目ID type 1复制 2转移
     * @return
     */
    @PutMapping("/questions-copy")
    public AjaxResult copyTransfer(@RequestBody QuestionDto question){
        if(question.getQuestionBankId() != null && question.getQuestionId() != null && question.getType() != null){

            int id = questionService.copyTransfer(question.getQuestionBankId(),question.getQuestionId(),question.getType());
            return AjaxResult.success(question.getQuestionBankId());
        }
        return AjaxResult.error("请确保传参不为空");
    }
    /**
     * 题目排序
     * @param question questionId 交换排序id1 questionIdTwo 交换排序id2
     * @return
     */
    @PutMapping("/questions-sort")
    public AjaxResult questionsSort(@RequestBody QuestionDto question){
        if(question.getQuestionId() != null && question.getQuestionIdTwo() != null){
            int result = questionService.questionsSort(question.getQuestionId(),question.getQuestionIdTwo());
            return AjaxResult.success(result);
        }
        return AjaxResult.error("请确保传参不为空");
    }
    /**
     * 查询题目详情
     * @param id
     * @return
     */
    @GetMapping("/questions/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find question by id:{}",id);
        Question result = questionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除题目
     * @param id
     * @return
     */
    @DeleteMapping("/questions/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete question by id:{}",id);
        int row = questionService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询题目
     * @param questionBankId 题库id
     * @param questionType 题目类型
     * @param stemType 题干类型
     * @param difficulty 题目难度
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/questions")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer questionBankId, @RequestParam(required = false) Integer questionType, @RequestParam(required = false) Integer stemType,@RequestParam(required = false) Integer category,@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer difficulty, @RequestParam(required = false) Integer questionState, Pageable pageable){
        log.info("Rest request to find question by params questionBankId:{},questionType:{},stemType:{},difficulty:{}", questionBankId, questionType, stemType, difficulty);

        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Question> questions = questionService.selectByParams(questionBankId, questionType, stemType,category,type, difficulty, null,questionState ,trainingOrganizationId);
        PageInfo<Question> result = new PageInfo<>(questions);
        return AjaxResult.success(result);
    }

    /**
     * 查询题目不分页
     * @param questionBankId 题库id
     * @param questionType 题目类型
     * @param stemType 题干类型
     * @param difficulty 题目难度
     * @return
     */
    @GetMapping("/questions-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer questionBankId,@RequestParam(required = false) Integer questionType,@RequestParam(required = false) Integer stemType,@RequestParam(required = false) Integer category,@RequestParam(required = false) Integer type,@RequestParam(required = false) Integer difficulty,@RequestParam(required = false) Integer materialQuestionId,@RequestParam(required = false) Integer questionState){
        log.info("Rest request to find question by params questionBankId:{},questionType:{},stemType:{},difficulty:{}", questionBankId, questionType, stemType, difficulty);
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        List<Question> result = questionService.selectByParams(questionBankId, questionType, stemType,category,type, difficulty, materialQuestionId,questionState ,trainingOrganizationId);
        return AjaxResult.success(result);
    }

}
